<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\library\Hash;
use app\common\library\Openssl;
use Endroid\QrCode\QrCode;
use think\Db;
use think\facade\Request;
use think\Validate;
use app\wx\controller\Index;
use think\facade\Cache;

class Info extends Base
{
    /**
     * 用户个人中心
     * @return mixed
     */
    public function user()
    {
        $info = app()->user;
        $this->assign('info', $info);
        //$this->assign('qrcode', $this->qrcode());
        app()->rbac->refresh();
        return view();
    }

    public function qrcode()
    {
        $info = Request::header();
        // dump($info);


        if (cache('bind' . session('uid') . 'time') > time()) {
            $qrcode = cache('bind' . session('uid') . 'qrcode');
        } else {
            $openssl = new Openssl();
            $data['uid'] =session('uid');
            $data['expires_in']=time()+60*10;
            $str = $openssl->encrypt(json_encode($data));
            cache('bind' . session('uid') . 'time', time() + 60 * 2);
            $url = url('/wx/wxbind/index?token=' . $str);
            $qrcode = $this->phpqrcode($url);
            cache('bind' . session('uid') . 'qrcode', $qrcode, time() + 60 * 2.5);
        }
        $this->error('获取二维码成功', $qrcode);//二维码

    }

    //生成二维码
    public function phpqrcode($url)
    {
        $file_name = 'qrcode';
        $qrCode = new QrCode();
        $qrCode->setText('https://' . Request::host() . '/' . $url);
        $qrCode->setSize(250);
        $qrCode->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0));
        $qrCode->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));
        $qrCode->setLabelFontSize(16);
        //ob_clean();
        //header('Content-Type: ' . $qrCode->getContentType());

        $file_path = $file_name . '/qrcode' . uniqid() . '.png';
        if (!file_exists($file_name)) {
            mkdir($file_name, 0777, true);
        }
        try {
            $qrCode->writeFile($file_path);
            //echo $file_path;
            return $file_path;
        } catch (\Exception $e) {
            return false;
        }

    }

    /**
     * 更新姓名
     */
    public function updateNickname()
    {
        $nickname = $this->request->param('nickname', '', 'trim');
        if (!$nickname) {
            $this->error('姓名不能为空');
        }
        if (Db::name('admin')->where('id', app()->user->id)->setField('nickname', $nickname) !== false) {
            app()->rbac->refresh();
            $this->success('修改成功');
        }
        $this->success('修改失败');
    }

    //更新头像
    public function updateFace()
    {
        $face = $this->request->param('face', '', 'trim');
        $face = ' /qiniu/downloadurl?file=' . $face;
        if (Db::name('admin')->where('id', app()->user->id)->setField('face', $face) !== false) {
            app()->rbac->refresh();
            $this->success('修改成功');
        }

        $this->success('修改失败');
    }

    //修改密码
    public function updatePassword()
    {
        $post = $this->request->param();
        $validate = new Validate([
            'old_password|密码' => 'require|length:6,16',
            'password|密码' => 'require|length:6,16',
            're_password|重复密码' => 'require|length:6,16'
        ], [
            'password.length' => '密码的长度为6到16位'
        ]);
        extract($post);
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }


        $admin = Admin::find(app()->user->id);
        if (!Hash::check($post['old_password'], $admin->password)) {
            $this->error('旧密码错误');
        }
        if ($post['password'] != $post['re_password']) {
            $this->error('两次密码输入不一致');
        }
        $password = Hash::hash($post['password']);
        if (Db::name('admin')->where('id', app()->user->id)->setField('password', $password) !== false) {
            $this->success('修改成功');
        }

        $this->success('修改失败');
    }
}